﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Blade.IO;
using Blade.Metadata.CliLoader.Metadata;

namespace Blade.Metadata.PeLoader {
	sealed class CliSectionHeader {
		public CliSectionHeader(BinaryReader stream, IMetadataLoaderContext context) {
			this.Signature = stream.ReadUInt32();
			context.Assert(() => Signature == 0x424A5342);
			this.MajorVersion = stream.ReadUInt16();
			context.Assert(() => MajorVersion == 1);
			this.MinorVersion = stream.ReadUInt16();
			context.Assert(() => MinorVersion == 1);
			this.Reserved = stream.ReadUInt32();
			context.Assert(() => Reserved == 0);
			var versionLength = stream.ReadInt32();
			context.Assert(() => versionLength <= 255);
			this.Version = stream.ReadString(versionLength, Encoding.UTF8);
			this.Flags = stream.ReadUInt16(); context.Assert(() => Flags == 0);
			var streamCount = stream.ReadUInt16();
			this.StreamHeaders = new CliStreamHeaderCollection();
			for (int i = 0; i < streamCount; i++) {
				StreamHeaders.Add(new CliStreamHeader(stream));
			}
		}

		public readonly uint Signature; // == 0x424A5342
		public readonly ushort MajorVersion; // == 1 -- ignore
		public readonly ushort MinorVersion; // == 1 -- ignore
		uint Reserved; // == 0
		//uint VersionLength;
		public readonly string Version;
		public readonly ushort Flags; // == 0
		//ushort StreamCount;
		public readonly CliStreamHeaderCollection StreamHeaders;
	}
}
